package com.db.compare.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import com.db.compare.exp.RbacEnumExp;
import com.db.compare.model.RbacEnum;
import com.db.compare.mapper.RbacEnumMapper;
import com.db.compare.service.RbacEnumService;
import com.db.compare.vo.RbacEnumVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class RbacEnumServiceImpl implements RbacEnumService {
    @Autowired
    private RbacEnumMapper rbacenumMapper;
    @Override
    public PageInfo<RbacEnum> pageInfo(RbacEnumVo rbacenumVo) {
        PageHelper.startPage(rbacenumVo.getPageNum(),rbacenumVo.getPageSize());
        List<RbacEnum> rbacenumList = rbacenumMapper.findList(rbacenumVo);
        PageInfo<RbacEnum> pageInfo = new PageInfo<>(rbacenumList);
        return pageInfo;
    }

    @Override
    public void download(String ids, HttpServletResponse response) throws Exception{
        String fileName = "枚举管理";
        try {
            ServletOutputStream outputStream=Appcontext.downloadResponse(response,fileName);
            if(StringUtils.isBlank(ids)){
                EasyExcel.write(outputStream, RbacEnumExp.class).sheet(fileName).doWrite(null);
            }else{
                String[] arrays = ids.split(",");
                List<RbacEnum> rbacenumList = rbacenumMapper.findByIds(arrays);
                List<RbacEnumExp> rbacenumExpList = new ArrayList<>();
                for(RbacEnum rbacenum:rbacenumList){
                    RbacEnumExp rbacenumExp = new RbacEnumExp();
                    BeanUtils.copyProperties(rbacenum,rbacenumExp);
                    rbacenumExpList.add(rbacenumExp);
                }
                EasyExcel.write(outputStream, RbacEnumExp.class).sheet(fileName).doWrite(rbacenumExpList);
            }
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    @Override
    public boolean upload(MultipartFile file) {
        ImportParams importParams = new ImportParams();
        try {
            ExcelImportResult<RbacEnumExp> result = ExcelImportUtil.importExcelMore(file.getInputStream(), RbacEnumExp.class,
                    importParams);
            List<RbacEnumExp> rbacenumExps = result.getList();
            for (RbacEnumExp rbacenumExp : rbacenumExps) {
                RbacEnum rbacenum = new RbacEnum();
                BeanUtils.copyProperties(rbacenum,rbacenumExp);
                int i = rbacenumMapper.insert(rbacenum);
                if(i < 1){
                    return false;
                }
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}
